<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="选择楼栋" prop="building_id">
        <el-select v-model="form.building_id" placeholder="请选择">
          <el-option
            v-for="item in buildList"
            :key="item.id"
            :label="item.building_name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所在楼层" prop="floor_no">
        <el-select
          v-model="form.floor_no"
          placeholder="请选择"
          @change="setFloorRooms"
        >
          <el-option
            v-for="item in 99"
            :key="item"
            :label="item + '层'"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="房间数" prop="number">
        <el-select
          v-model="form.number"
          placeholder="请选择"
          @change="setFloorRooms"
        >
          <el-option
            v-for="item in 99"
            :key="item"
            :label="item + '间'"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="房间号">
        <div class="roomsNumber">{{ form.floor_rooms_no }}</div>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>
<script>
  import { getBuildList, addFloor } from '@/api/room'
  export default {
    name: 'AddFloor',
    data() {
      return {
        form: {
          building_id: null,
          floor_no: 1,
          number: 1,
          floor_rooms_no: '101',
        },
        rules: {
          building_id: [
            { required: true, trigger: 'blur', message: '请选择楼栋名称' },
          ],
          floor_no: [
            { required: true, trigger: 'blur', message: '请选择楼层' },
          ],
          number: [
            { required: true, trigger: 'blur', message: '请选择房间数' },
          ],
        },
        title: '添加楼层',
        dialogFormVisible: false,
        buildList: [],
      }
    },
    methods: {
      showEdit() {
        this.getBuild()
        this.dialogFormVisible = true
      },
      close() {
        this.dialogFormVisible = false
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { data } = await addFloor(this.form)
            if (data.is_success) {
              this.$baseMessage(
                '添加成功',
                'success',
                'vab-hey-message-success'
              )
              this.$emit('fetch-data')
              this.close()
            }
          }
        })
      },
      async getBuild() {
        const { data } = await getBuildList({})
        this.buildList = data
      },
      setFloorRooms() {
        var rooms = []
        for (let i = 0; i < this.form.number; i++) {
          var str = String(i + 1).padStart(2, 0)
          var name = this.form.floor_no + str
          rooms.push(name)
        }
        this.form.floor_rooms_no = rooms.join(',')
      },
    },
  }
</script>
<style lang="scss" scoped>
  .roomsNumber {
    width: 300px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
</style>
